package org.example.langchan4j.config;


import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class AiCodeHelperServiceFactory {
    @Resource
    private QwenChatModel qwenChatModel;
    @Bean
    public AiCodeHelperService aiCodeHelperService() {//二选一
        // 创建AiCodeHelperService 使用aiService的时候创建的

//            AiServices.create(AiCodeHelperService.class, qwenChatModel);


        // 内存会话记忆
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);//10条记忆
        AiCodeHelperService aiCodeHelperService = AiServices.builder(AiCodeHelperService.class)
                .chatModel(qwenChatModel)
                .chatMemory(chatMemory)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                .build();
        return aiCodeHelperService;
    }

    //数据库会话记忆
//    @Bean
//    public AiCodeHelperService aiCodeHelperService(PersistentChatMemoryStore persistentChatMemoryStore) {
//        AiCodeHelperService aiCodeHelperService = AiServices.builder(AiCodeHelperService.class)
//                .chatModel(qwenChatModel)
//                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.builder()
//                        .chatMemoryStore(persistentChatMemoryStore) // 使用数据库存储
//                        .maxMessages(10)
//                        .id(memoryId)
//                        .build())
//                .build();
//        return aiCodeHelperService;
//    }
//
//
//    @Bean
//    public PersistentChatMemoryStore persistentChatMemoryStore() {
//        return new PersistentChatMemoryStore(jdbcTemplate);
//    }
}
